package dome1;

public class MySingleList {
    //静态内部类
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    //创建链表
    public void createList() {
        ListNode list0 = new ListNode(1);
        ListNode list1 = new ListNode(2);
        ListNode list2 = new ListNode(3);
        ListNode list3 = new ListNode(4);
        ListNode list4 = new ListNode(5);
        list0.next = list1;
        list1.next = list2;
        list2.next = list3;
        list3.next = list4;
        head = list0;
    }

    //打印链表
    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

    //返回链表长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //是否包含key
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        ListNode list = new ListNode(data);
        list.next = head;
        head = list;
    }

    //尾插法
    public void addLast(int data) {
        ListNode list = new ListNode(data);
        if (head == null) {
            head = list;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = list;
    }
    //任意位置插入
    public void addIndex(int index, int data) {
        if(index<0||index>size()) {
            System.out.println("index不合法！");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode list = new ListNode(data);
        list.next = cur.next;
        cur.next = list;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while(count!=index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head==null) {
            return;
        }
        if(head.val==key) {
            head = head.next;
        }
        ListNode cur = fingKey(key);
        if(cur==null) {
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode fingKey(int key) {
        ListNode cur = head;
        while(cur.next!=null) {
            if(cur.next.val==key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null) {
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur!=null) {
            if(cur.val==key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val==key) {
            head = head.next;
        }
    }
    //清空链表
    public void clear() {
        ListNode cur = head;
        while(cur!=null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
}